// const cons = require('consolidate');
const program = require('commander');
const path = require('path');
const { version } = require('./constants');
// const cons = require('consolidate');

const mapActions = {
  create: {
    alias: 'c',
    description: '',
    action: () => {},
    examples: ['zhu-cli create <project-name>'],
  },
};

Reflect.ownKeys(mapActions).forEach((key) => {
  const action = mapActions[key];
  program
    .command(key)
    .alias(action.alias)
    .description(action.description)
    .action(() => {
      if (key === '*') {
        console.log(action.description);
      } else {
        console.log(key);
        // 动态引入命令对应的脚本文件，并执行
        require(path.resolve(__dirname, key))(...process.argv.slice(3));
      }
    });
});

program.on('--help', () => {
  Reflect.ownKeys(mapActions).forEach((key) => {
    mapActions[key].examples.forEach((example) => {
      console.log(`    ${example}`);
    });
  });
});

// zhu-cli --version 命令
// 解析用户传过来的参数  zhu-cli --help 运行出来结果
program.version(version).parse(process.argv);
